package com.comunity_cloud.client.model.fetch_file;

import java.io.UnsupportedEncodingException;

import com.comunity_cloud.client.model.communication.packet.PeerPacketHeader;

public class FetchFileHeader extends PeerPacketHeader{

	private long version;
	
	private String fileName;

	private byte[] fileNameBytes;
	
	public FetchFileHeader() {
		this.type = Type.FETCH_FILE;
	}
	
	@Override
	protected void convertToBytes() {
		byteBuffer.putLong(version);
		byteBuffer.put(fileNameBytes);
	}

	@Override
	protected void fromBytes() {
		version = byteBuffer.getLong();
		fileName = new String(bytes,byteBuffer.position(),headerLength - 8);
	}
	
	@Override
	protected int headerLength() {
		int length = 0;
		try {
			fileNameBytes = fileName.getBytes("UTF8");
			length = fileNameBytes.length;
		} catch (UnsupportedEncodingException e) {}
		
		return 8 + length;
	}

	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
